#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>

int main() {
    int subprocess_status;
    printf("父进程:PID为%d\n", getpid());
    pid_t pid = fork();
    if (pid < 0) {
        perror("When fork");
        return 1;
    }
    else if (pid == 0) {
        char *args[] = {"/usr/bin/ping", "-c", "10", "www.baidu.com", NULL};
        char *envs[] = {NULL};
        printf("子进程执行:PID为%d\n",getpid());
        int res = execve(args[0], args, envs);
        if (res < 0) {
            perror("When execve");
            return 1;
        }
    }
    else {
        printf("父进程%d等待子进程%d退出\n", getpid(), pid);
        waitpid(pid, &subprocess_status, 0);
    }
    printf("父进程%d已等待子进程%d结束\n", getpid(), pid);
    return 0;
}